<?php
namespace application\datatypes;
use application\datatypes\DataType;
class DateTime extends DataType {
	public static $name = 'Date & Time';
	public static $fields = array(array('columnType'=>'DATETIME'));
	
	public static $displayParams = array(
		array(
			'label'=>'Format',
			'name'=>'format',
			'type'=>'text',
			'defaultValue'=>'m/d/Y g:ia'
		)
	);
	
	public static function validate($field, $data, $results) {
		if($field->cf_required && (!$data[$field->cf_column .'_date']) || (!$data[$field->cf_column . '_time'])) {
			$results['errors'][] = '<b>'.$field->cf_display_name.'</b> (date & time) is required.';
		}
		elseif($data[$field->cf_column . '_date'] && $data[$field->cf_column . '_time'] && strtotime($data[$field->cf_column . '_date'] .' '.$data[$field->cf_column . '_time'])) {
			$datetime = strtotime($data[$field->cf_column . '_date'] . ' ' . $data[$field->cf_column . '_time']);
			if(!$datetime) $results['errors'][] = '<b>'.$field->cf_display_name.'</b> must have a valid date and time.';
			else $results['db_fields'][$field->cf_column] = date('Y-m-d H:i:s', $datetime);
		}
		
		return $results;
	}
	
	public static function formHeader($field) {
		Modulator()->includeScript('jquery');
		Modulator()->includeScript('jquery ui');
		Modulator()->includeScript(getCommonFile('third-party/jquery.timeentry/jquery.timeentry.js'));
		?>
		<script type="text/javascript">
			$(document).ready(function() {
				$('.datetime-datepicker').datepicker();
				$('.datetime-timepicker').timeEntry();
			});
		</script>
		<?php
	}
	
	public static function display($formElements) {
		?>
		
		<fieldset class="date-time-field">
			<header><?=$formElements['display']?></header>
			<section class="inner">
			<?php if($description = $formElements['description']) : ?>
				<p class="form-description"><?=$description?></p>
				<?php endif;?>
			<label>Date</label>
			<?=$formElements['elements']['date']?>
			<label>Time</label>
			<?=$formElements['elements']['time']?>
			</section>
		</fieldset>
		<?php
	}
	
	public static function build($field, $data) {
		$date = (strtotime($data[$field->cf_column]) > 0) ? date('m/d/Y', strtotime($data[$field->cf_column])) : '';

		$date = (strtotime($data[$field->cf_column]) > 0) ? date('h:iA', strtotime($data[$field->cf_column])) : '';

		$formElements = array('description'=>$field->cf_description, 'column'=>$field->cf_column, 'display'=>$field->cf_display_name, 'data_type'=>$field->cf_data_type, 'elements'=>array(
				'date'=>
					sprintf('<input type="text" value="%1$s" name="%2$s" id="%2$s" class="datetime-datepicker"/>', $date, $field->cf_column.'_date'),
				'time'=>
					sprintf('<input type="text" value="%1$s" name="%2$s" id="%2$s" class="datetime-timepicker"/>', $time, $field->cf_column.'_time'),
					
				));
		return $formElements;
	
	}
	
	public static function prepareData($field, $data, $params) {
		if(!$params['format']) $params['format'] = 'm/d/Y g:ia';
		$datetime = $data[$field->cf_column.'_date'].' '.$data[$field->cf_column.'_time'];
		return date($params['format'], strtotime($datetime));
	}
	
	public static function fromDB($fields, $field, $data) {
		$column = $field->cf_column;
		return array($field->cf_column.'_date' => date('m/d/Y', strtotime($fields[$column])), $field->cf_column.'_time' => date('g:ia', strtotime($fields[$column])));
	}
	

	
	public static function save() {
		$date = $_REQUEST['field_default_date'];
		$time = $_REQUEST['field_default_time'];
		$field = $_REQUEST['field'];
		DB()->query("UPDATE `cn_content_fields` SET `cf_default`='$date $time' WHERE `cf_id`='$field'");
	}
	
}
?>